/**
 * Copyright 2010-present Facebook.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.twodstar.selfie;


import java.io.File;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.parse.ParseObject;
import com.parse.ParseUser;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;


/**
 * Base class for a list element in the Scrumptious main display, consisting of an
 * icon to the left, and a two line display to the right.
 */
public class Element_BattleList 
{
	private static final int CAMERA = 0;
    private static final int GALLERY = 1;
    
    public static final int REQUEST_CAMERA = 512;
    public static final int REQUEST_PHOTO = 513;
    
    private static final String FILE_PREFIX = "selfie_img_";
    private static final String FILE_SUFFIX = ".jpg";
    
    public static ParseObject m_CounterAttackParseObject = null;
     
	private Activity m_Activity;
    private BaseAdapter adapter;
    private Fragment m_HostFragment;
    private int m_Index = -2;

    private ParseObject m_ParseObj;
	private String m_PlayerID1;
	private String m_PlayerID2;
    private String m_URL_P1;
    private String m_URL_P2;
    private String m_PhotoID1;
    private String m_PhotoID2;    
    private String m_PlayerName1;
    private String m_PlayerName2;
    private String m_WinnerID;
    private int m_InitialLike_P2;
    private int m_Finished_Like1;
    private int m_Finished_Like2;
    private long m_StartDate;
    private long m_EndDate;
    private boolean m_IsCounterAttacked;
      
    private boolean m_IsFinished;   
    
    private int m_LikeCount1;
    private int m_LikeCount2;
    private int m_BattleLike2;
    private boolean m_SavedWinnerID;
    
    private boolean m_IsCanQueryPhotoLike1 = false;
    private boolean m_IsCanQueryPhotoLike2 = false;
    private boolean m_IsCanCulateLike = false;
    
    private ListView m_ListView;
    private TextView m_TextView_TotalLike1;
    private TextView m_TextView_TotalLike2;
    private TextView m_TextView_BattleLike1;
    private TextView m_TextView_BattleLike2;
    private TextView m_TextView_CountDown;
    private ProgressBar m_ProgressBar;
    private Button m_BtnCounterAttack; 
    private ImageView	m_ImgOfficail;
    
    private static Uri tempUri = null;
  
  
    
    /**
     * Constructs a new list element.
     *
     * @param icon the drawable for the icon
     * @param text1 the first row of text
     * @param text2 the second row of text
     * @param requestCode the requestCode to start new Activities with
     */
    public Element_BattleList(Activity PActivity, ListView pListView, Fragment pFragment, ParseObject pParseObj, int Index,
    						 String PlayerID1, String PlayerID2,
    						 String URL_P1, String URL_P2, String PhotoID1, String PhotoID2, 
    						 String PlayerName1, String PlayerName2, String WinnerID, 
    						 int InitialLike_P2, int Finished_Like1, int Finished_Like2,
    						 long StartDate, long EndDate,
    						 boolean IsCounterAttacked
    						) 
    {
    	m_Activity = PActivity;
    	m_ListView = pListView; 
    	m_HostFragment = pFragment;
    	m_Index = Index;
    	
    	m_ParseObj = pParseObj;
    	m_PlayerID1 = PlayerID1;
    	m_PlayerID2 = PlayerID2;
    	m_URL_P1 = URL_P1;
    	m_URL_P2 = URL_P2;
    	m_PhotoID1 = PhotoID1;
        m_PhotoID2 = PhotoID2;    
    	m_PlayerName1 = PlayerName1;
    	m_PlayerName2 = PlayerName2;    
    	m_WinnerID = WinnerID;
    	m_InitialLike_P2 = InitialLike_P2;   	
    	m_Finished_Like1 = Finished_Like1;
    	m_Finished_Like2 = Finished_Like2;
    	m_StartDate = StartDate;
    	m_EndDate = EndDate;
    	m_IsCounterAttacked = IsCounterAttacked;
    	
    	
    	m_LikeCount1 = 0;
    	m_LikeCount2 = 0;
    	
    	m_IsFinished = false;  	
        m_SavedWinnerID = false;
        
    	//Log.i("game", "m_Finished_Likes " + m_Finished_Like1 + " " + m_Finished_Like2);
    	if(WinnerID.equalsIgnoreCase("0") == true)
    	{    		
    	}
    	else
    	{
    		m_IsFinished = true;    		
    	}
       
    }
    
    public static void Reset()
    {
    	m_CounterAttackParseObject = null;
    }
    
   
    
    public void SetViewElements(    							 
    							 TextView TextView_TotalLike1,
								 TextView TextView_TotalLike2,
								 TextView TextView_BattleLike1,
								 TextView TextView_BattleLike2,
								 TextView TextView_CountDown,
								 ProgressBar PB,
								 Button BtnCounterAttack,
								 ImageView ImgOfficial
								 )
    {
    	//Log.i("game", "SetViewElements-----------------");
    	
    		
    	m_TextView_TotalLike1 = TextView_TotalLike1;
        m_TextView_TotalLike2 = TextView_TotalLike2;
        m_TextView_BattleLike1 = TextView_BattleLike1;
        m_TextView_BattleLike2 = TextView_BattleLike2;
        m_TextView_CountDown = TextView_CountDown;
        m_ProgressBar = PB;
        m_BtnCounterAttack = BtnCounterAttack;
        m_ImgOfficail = ImgOfficial;
        
        
        
        
        
        if(m_IsCanQueryPhotoLike1 && m_IsCanQueryPhotoLike2)
        {
        	m_IsCanCulateLike = true;
        }
        
        SetLikeCalculation();
        UpdateTimer();
        ShowWinner();
        
        if(m_BtnCounterAttack != null)
    	{
	        if(GetCanCounterAttack())
	        {	        	
        		if(m_BtnCounterAttack.getVisibility() == View.GONE)            	            		
            		m_BtnCounterAttack.setVisibility(View.VISIBLE);
        		
        			m_BtnCounterAttack.setText("Counter Attack");
                	m_BtnCounterAttack.setOnClickListener(getOnClickCounterAttack());
	        	
	        }
	        else if(GetCanChat())
	        {
	        	if(m_BtnCounterAttack.getVisibility() == View.GONE)            	            		
            		m_BtnCounterAttack.setVisibility(View.VISIBLE);
	        	
	        	if(m_BtnCounterAttack != null)
	            {
	        		m_BtnCounterAttack.setText("     Chat    ");
	            	m_BtnCounterAttack.setOnClickListener(getOnClickChat());
	            	
	            }
	        }
    	}
        
        if(m_ImgOfficail != null)
        {
        	if(m_IsCounterAttacked == false)
        	{
        		m_ImgOfficail.setVisibility(View.GONE);
        	}
        }
        
        //notifyDataChanged()
    }
    
    private boolean GetCanChat()
    {	
    	if(IsMe(m_PlayerID1) && m_IsFinished == false && SelfieBattleApplication.Instance().GetIsFacebookInstalled()) 
    	{
    		return true;
    	}
    	return false;
    }
    
    private void ShowWinner()
    {
    	if(m_IsFinished && m_TextView_CountDown != null)
        {
        	if(IsMe(m_WinnerID))
        	{
        		m_TextView_CountDown.setText(R.string.youwon);
        	}
        	else
        	{
        	        	
	        	if(m_WinnerID.equalsIgnoreCase(m_PlayerID1) == true)
	        	{        		
	        		m_TextView_CountDown.setText(R.string.winner);
	        		m_TextView_CountDown.setText(m_TextView_CountDown.getText() + " " +  m_PlayerName1 );
	        	}
	        	else if(m_WinnerID.equalsIgnoreCase(m_PlayerID2) == true)
	        	{
	        		m_TextView_CountDown.setText(R.string.winner);
	        		m_TextView_CountDown.setText(m_TextView_CountDown.getText() + " " + m_PlayerName2 );
	        	}
	        	else if((m_WinnerID.equalsIgnoreCase("1") == true))
	        		m_TextView_CountDown.setText(R.string.draw);
        	}
        	
        	if(m_TextView_CountDown.getVisibility() == View.GONE)
        	{        		
        		m_TextView_CountDown.setVisibility(View.VISIBLE);
        	}
        	
        }
    }
    
 
    
    private boolean IsMe(String ThisID)
    {
    	ParseUser PU = ParseUser.getCurrentUser();    	
    	if(PU != null)
    	{
    		String MyID = PU.getString("FBID");
    		if(MyID != null && MyID.isEmpty() == false)
    		{
    			if(MyID.equalsIgnoreCase(ThisID))
    				return true;
    		}
    	}
    	
    	return false;
    }

    /**
     * The Adapter associated with this list element (used for notifying that the
     * underlying dataset has changed).
     * @param adapter the adapter associated with this element
     */
    public void setAdapter(BaseAdapter padapter) 
    {
        this.adapter = padapter;
    }
    
    public BaseAdapter getAdapter() 
    {
        return this.adapter;
    }

    public ParseObject getParseObj()
    {
    	return m_ParseObj;
    }
      
    public String getPlayerID1() 
    {
        return m_PlayerID1;
    }
    
    public String getPlayerID2() 
    {
        return m_PlayerID2;
    }
    
    public String getURL_P1() 
    {
        return m_URL_P1;
    }
    
    public String getURL_P2() 
    {
        return m_URL_P2;
    }
    
    public String getPhotoID1() 
    {
        return m_PhotoID1;
    }
    
    public String getPhotoID2() 
    {
        return m_PhotoID2;
    }
    
    public String getPlayerName1() 
    {
        return m_PlayerName1;
    }
    
    public String getPlayerName2() 
    {
        return m_PlayerName2;
    }
    
    public int getInitialLikeP2()
    {
    	return m_InitialLike_P2;
    }
    
    public long getStartDate()
    {
    	return m_StartDate;
    }
    
    public long getEndDate()
    {
    	return m_EndDate;
    }
    
    
    
    
    public void SetLikeCount1(int LikeCount)
    {
    	m_LikeCount1 = LikeCount; 
    	m_IsCanQueryPhotoLike1 = true;
		if(m_IsCanQueryPhotoLike1 && m_IsCanQueryPhotoLike2)
		{
			m_IsCanCulateLike = true;
		}
         
    	
    	SetLikeCalculation();
    	
    	//Log.i("game", "LikeCount " + m_PhotoID1 + " "  +  LikeCount);
    }
    
    public void SetLikeCount2(int LikeCount)
    {
    	m_LikeCount2 = LikeCount;
    	m_IsCanQueryPhotoLike2 = true;
    	 if(m_IsCanQueryPhotoLike1 && m_IsCanQueryPhotoLike2)
         {
         	m_IsCanCulateLike = true;
         }
         
    	
    	SetLikeCalculation();
    	
    	//Log.i("game", "LikeCount " + m_PhotoID2 + " "  +  LikeCount);
    }
    
    private void SetLikeCalculation()
    {
    	    	
    	if(m_IsFinished)
    	{
    		SetLikeCalculationForFinishedBattle();
    		return;
    	}
    	
    	if(m_IsCanCulateLike == false)
    		return;
    	
    	 //R.id.LikeTotal1
         //R.id.LikeTotal2
         //R.id.LikeBattle1
         //R.id.LikeBattle2
    	
    	if(m_TextView_TotalLike1 != null)
    	{
    		//Log.i("game", "LikeCount " + m_PhotoID1 + " "  +  m_LikeCount1);
    		
    		m_TextView_TotalLike1.setText(R.string.totallike);
    		m_TextView_TotalLike1.setText(m_TextView_TotalLike1.getText().toString() + " " + m_LikeCount1);
    		UpdateListViewPartially(m_Index, R.id.LikeTotal1, m_TextView_TotalLike1.getText().toString());
    		
    		if(m_TextView_TotalLike1.getVisibility() == View.GONE)
        	{        		
        		m_TextView_TotalLike1.setVisibility(View.VISIBLE);
        	}
    		
    	}
    	else
    	{
    		//Log.i("game", "m_TextView_TotalLike1 is null ");
    	}
    	
    	if(m_TextView_BattleLike1 != null)
    	{
    		m_TextView_BattleLike1.setText("");
    		
    	}
    	
    	
    	m_BattleLike2 = 0;
    	if(m_TextView_TotalLike2 != null)
    	{
    		
    		m_TextView_TotalLike2.setText(R.string.totallike);
    		m_TextView_TotalLike2.setText(m_TextView_TotalLike2.getText().toString() + " " + m_LikeCount2);
    		UpdateListViewPartially(m_Index, R.id.LikeTotal2, m_TextView_TotalLike2.getText().toString());
    		
    		
    		m_BattleLike2 = m_LikeCount2 -  (int)( m_InitialLike_P2 * 0.5);
    		if(m_BattleLike2 < 0)
    			m_BattleLike2 = 0;
    		
    		m_TextView_BattleLike2.setText(R.string.battlelike);
    		m_TextView_BattleLike2.setText(m_TextView_BattleLike2.getText().toString() + " " + m_BattleLike2);
    		UpdateListViewPartially(m_Index, R.id.LikeBattle2, m_TextView_BattleLike2.getText().toString());
    		
    		
    		if(m_TextView_TotalLike2.getVisibility() == View.GONE)
        	{        		
        		m_TextView_TotalLike2.setVisibility(View.VISIBLE);
        	}
    		
    		if(m_TextView_BattleLike2.getVisibility() == View.GONE)
        	{        		        		
        		m_TextView_BattleLike2.setVisibility(View.VISIBLE);
        	}
    	}
    	else
    	{
    		//Log.i("game", "m_TextView_TotalLike2 is null ");
    	}
    	
    	if(m_ProgressBar != null)
    	{
    		float Ratio = 0;
    		if( m_LikeCount1 + m_BattleLike2 > 0)
    		{
    			Ratio = (float)m_LikeCount1 / (float)(m_LikeCount1 + m_BattleLike2);    			
    		}
    		else if( m_LikeCount1 == 0)
    		{
    			Ratio = 0.5f;
    		}
    		else
    		{
    			Ratio = 1;
    		}
    		int P = (int)(Ratio * 100);
    		//Log.i("game", "Progress " + P);
    		m_ProgressBar.setProgress(P);
    		if(m_ProgressBar.getVisibility() == View.GONE)
        	{        	   
        		m_ProgressBar.setVisibility(View.VISIBLE);
        	}
    	}
    	
    	
    }
    
    private void SetLikeCalculationForFinishedBattle()
    {
    	if(m_TextView_TotalLike1 != null)
    	{
    		m_TextView_BattleLike1.setText(R.string.battlelike);
    		m_TextView_BattleLike1.setText(m_TextView_BattleLike1.getText().toString() + " " + m_Finished_Like1);
    		
    		if(m_TextView_BattleLike1.getVisibility() == View.GONE)
        	{        		 
        		m_TextView_BattleLike1.setVisibility(View.VISIBLE);
        	}
    	}
    	
    	if(m_TextView_TotalLike2 != null)
    	{
    		m_TextView_BattleLike2.setText(R.string.battlelike);  		
    		m_TextView_BattleLike2.setText(m_TextView_BattleLike2.getText().toString() + " " + m_Finished_Like2);
    		if(m_TextView_BattleLike2.getVisibility() == View.GONE)
        	{        		 
        		m_TextView_BattleLike2.setVisibility(View.VISIBLE);
        	}
    	}
    	
    	if(m_ProgressBar != null)
    	{
    		float Ratio = 0;
    		if(m_Finished_Like1 + m_Finished_Like2  > 0)
    		{
    			Ratio = (float)m_Finished_Like1 / (float)(m_Finished_Like1 + m_Finished_Like2);    	
    			//Log.i("game", "Ratio " + Ratio);
    		}
    		else if( m_Finished_Like1 == 0)
    		{
    			Ratio = 0.5f;
    		}
    		else
    		{
    			Ratio = 1;
    		}
    		int P = (int)(Ratio * 100);
    		
    		m_ProgressBar.setProgress(P);
    		if(m_ProgressBar.getVisibility() == View.GONE)
        	{        		 
        		m_ProgressBar.setVisibility(View.VISIBLE);
        	}
    	}
    	
    	ShowWinner();
    	
    }
    
      
    protected View.OnClickListener getOnClickListenerLeft()
    {
    	 return new View.OnClickListener() 
         {
             @Override
             public void onClick(View view)
             {
                 //showPhotoChoice();
            	 
            	String url;
            	
            	if(SelfieBattleApplication.Instance().GetIsFacebookInstalled())
            	{
            		url = "fb://photo/" +  m_PhotoID1;
            	}
            	else
            	{
            		url = "https://m.facebook.com/" +  m_PhotoID1;
            	}
            	
         		Intent i = new Intent(Intent.ACTION_VIEW);
         		i.setData(Uri.parse(url));
         		m_Activity.startActivity(i);
         		
            	 
             }
         };
    }
    
    protected View.OnClickListener getOnClickListenerRight()
    {
    	 return new View.OnClickListener() 
         {
             @Override
             public void onClick(View view)
             {
            	String url = "fb://photo/" +  m_PhotoID2;
          		Intent i = new Intent(Intent.ACTION_VIEW);
          		i.setData(Uri.parse(url));
          		m_Activity.startActivity(i);
             }
         };
    }
    
    
    private View.OnClickListener getOnClickCounterAttack()
    {
    	return new View.OnClickListener() 
        {
            @Override
            public void onClick(View view)
            {
            	//String url = "fb://photo/" +  m_PhotoID2;
         		//Intent i = new Intent(Intent.ACTION_VIEW);
         		//i.setData(Uri.parse(url));
         		//m_Activity.startActivity(i);
            	m_CounterAttackParseObject = m_ParseObj;
            	OnClickCounterAttack();
            }
        };
    }
    
    private View.OnClickListener getOnClickChat()
    {
    	return new View.OnClickListener() 
        {
            @Override
            public void onClick(View view)
            {
            	OnClickChat();
            }
        };
    }
    
    private void OnClickChat()
    {
    	
    	String url = "fb://messaging/compose/" + m_PlayerID2;
 		Intent i = new Intent(Intent.ACTION_VIEW);
 		i.setData(Uri.parse(url));
 		//i.putExtra("extra_publish_params", "test message");
 		//i.putExtra("composer_extras", "test message");
 		//i.putExtra("text", "test message");
 		//i.putExtra("compose", "test message");
 		
 		//i.setType(HTTP.PLAIN_TEXT_TYPE);

 		if(this.m_Activity != null)
 		{
 			m_Activity.startActivity(i);
 		}
    	
    }
    
    
    private void OnClickCounterAttack()
    {
    	 AlertDialog.Builder builder = new AlertDialog.Builder(m_Activity);
         CharSequence camera = m_Activity.getResources().getString(R.string.action_photo_camera);
         CharSequence gallery = m_Activity.getResources().getString(R.string.action_photo_gallery);
         builder.setCancelable(true).
                 setItems(new CharSequence[] {camera, gallery}, new DialogInterface.OnClickListener()
                 {
                     @Override
                     public void onClick(DialogInterface dialogInterface, int i) 
                     {
                         if (i == CAMERA) 
                         {
                             startCameraActivity();
                         } 
                         else if (i == GALLERY) 
                         {
                             startGalleryActivity();
                         }
                     }
                 });
         builder.show();
    }
    
    private void startCameraActivity() 
    {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        tempUri = getTempUri();
        if (tempUri != null) 
        {
            intent.putExtra(MediaStore.EXTRA_OUTPUT, tempUri);
        }
        m_HostFragment.startActivityForResult(intent, REQUEST_CAMERA);
    }

    private void startGalleryActivity() 
    {
        tempUri = null;
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        String selectPicture = m_Activity.getResources().getString(R.string.select_picture);
        m_HostFragment.startActivityForResult(Intent.createChooser(intent, selectPicture), REQUEST_PHOTO);
    }
    
    public static Uri GetResultURI()
    {
    	return tempUri;
    }
    
    private Uri getTempUri()
    {
        String imgFileName = FILE_PREFIX + System.currentTimeMillis() + FILE_SUFFIX;

        // Note: on an emulator, you might need to create the "Pictures" directory in /mnt/sdcard first
        //       % adb shell
        //       % mkdir /mnt/sdcard/Pictures
        File image = new File(
                Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), imgFileName);
        return Uri.fromFile(image);
    }
   
     
    
    
    public void LoadImageFromURL(ImageView image, Boolean IsP1)
    {
    	
    	String image_url = "";
    	
    	if(IsP1)
    		image_url = m_URL_P1;
    	else
    		image_url = m_URL_P2;
    	
    	// Get singletone instance of ImageLoader
    	
        ImageLoader imageLoader = ImageLoader.getInstance();
        // Initialize ImageLoader with configuration. Do it once.
        imageLoader.init(ImageLoaderConfiguration.createDefault(image.getContext()) );
        // Load and display image asynchronously
 
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                    .showStubImage(R.drawable.action_people) //this is the image that will be displayed if download fails
		            .cacheInMemory()
		            .cacheOnDisc()
		            .build();
 
        imageLoader.displayImage(image_url, image, options);
    	
    }
    
    public void Tick()
    {
    	long Now =  SntpClient.GetTimeUTC();
    	long CountDown = m_EndDate  - Now;    	
    	
    	if(CountDown <= 0)
    	{
    		if(m_SavedWinnerID == false && m_ParseObj != null)
    		{
    			m_SavedWinnerID = true;
	    		// save it to database and refresh battle list
    			
    			if(GetShouldSave())
    			{
		    		String WinnerID = "1";
		    		if(m_LikeCount1 > m_BattleLike2)
		    			WinnerID = m_PlayerID1;
		    		else if(m_LikeCount1 < m_BattleLike2)
		    			WinnerID = m_PlayerID2;
		    		
		    		PBUtility.LOG("Updating WinnerID -------------------------------");
		    		DataManager.UpdateWinnerID( m_ParseObj.getObjectId() , WinnerID , m_PlayerID1, m_PlayerID2, m_LikeCount1, m_BattleLike2);
    			}
    			else
    			{
    				// Hide It?
    			}
    		}
    		
    		// now remove me.
    		
    		return;
    	}
    	
    	long TotalSecond = CountDown / 1000;
    	long Hour = TotalSecond / 3600;
    	
    	long Minute = (TotalSecond % 3600) / 60;
    	long Second =  (TotalSecond % 3600) % 60;
    	
    	if(m_TextView_CountDown != null)
    	{
	    	m_TextView_CountDown.setText(R.string.countdown);
	    	m_TextView_CountDown.setText(m_TextView_CountDown.getText().toString() + " " + Hour + ":" + Minute + ":" + Second);
	    	UpdateListViewPartially(m_Index , R.id.BattleCountDown, m_TextView_CountDown.getText().toString());
    	}
    	
    	//notifyDataChanged();
    }
    
    private void UpdateTimer()
    {
    	long Now =  SntpClient.GetTimeUTC();
    	long CountDown = m_EndDate  - Now;    	
    	
    	long TotalSecond = CountDown / 1000;
    	long Hour = TotalSecond / 3600;
    	
    	long Minute = (TotalSecond % 3600) / 60;
    	long Second =  (TotalSecond % 3600) % 60;
    	
    	if(CountDown < 0)
    	{
    		if(m_TextView_CountDown != null)
    			m_TextView_CountDown.setVisibility(View.GONE);
    		
    		return;
    	}
    	
    	if(m_TextView_CountDown != null && m_IsFinished == false)
    	{
    		m_TextView_CountDown.setText(R.string.countdown);
    		m_TextView_CountDown.setText(m_TextView_CountDown.getText().toString()  + " "  + Hour + ":" + Minute + ":" + Second );
    		
    		if(m_TextView_CountDown.getVisibility() == View.GONE)
        	{        		
        		m_TextView_CountDown.setVisibility(View.VISIBLE);
        	}
    		//Log.i("game","m_TextView_CountDown " + Hour + ":" + Minute + ":" + Second );
    		//Log.i("game",m_TextView_CountDown.getText().toString() );
    	}
    	else
    	{
    		//Log.i("game","m_TextView_CountDown is null");
    	}
    }
    
    private boolean GetShouldSave()
    {
    	
    	String FBID = SelfieBattleApplication.Instance().GetFBID();
    	
    	if(FBID.equalsIgnoreCase ( m_PlayerID1  ))
    	{
    		return true;
    	}
    	if(FBID.equalsIgnoreCase ( m_PlayerID2  ))
    	{
    		return true;
    	}
    	
    	return false;
    }
    
    public void SaveWinner()
    {
    	
    }
    
    // Fix me
    public void RemoveIfPhotoNotAvailable()
    {
    	
    }
    
    private boolean GetCanCounterAttack()
    {
    	long Now =  SntpClient.GetTimeUTC();
    	long CountDown = m_EndDate  - Now;    	
    	
    	if(IsMe(m_PlayerID2) && m_IsFinished == false && m_IsCounterAttacked == false && CountDown > 60 * 1000 * 60 )
    	{
    		return true;
    	}
    	return false;
    }
    
    public void HideCounterAttackButton()
    {
    	if(m_BtnCounterAttack !=null)
    	{
    		if(m_BtnCounterAttack.getVisibility() == View.GONE)
        	{        		 
        		m_BtnCounterAttack.setVisibility(View.GONE);
        	}
    	}
    }
    
    private void UpdateListViewPartially(int itemIndex, int ID,  String Str)
    {
		 if(m_ListView == null)
			 return;
		 
		 if(itemIndex < 0 )
			 return;
		 
        int visiblePosition = m_ListView.getFirstVisiblePosition();
        View v = m_ListView.getChildAt(itemIndex - visiblePosition);
 
        if(v != null)
        {
        	TextView TV = (TextView) v.findViewById(ID);
        	if(TV != null)
        		TV.setText(Str);
        }
        
    }
    
}
